import { Component, OnInit, Input, ViewChild } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { formInput, controlType } from 'app/public/components/form-control/form-control';
// import { ViewChild } from '@angular/core/src/metadata/di';

@Component({
  selector: 'app-form-control',
  templateUrl: './form-control.component.html',
  styleUrls: ['./form-control.component.css'],
  preserveWhitespaces: true
})
export class FormControlComponent implements OnInit {
  constructor() { }
  ngOnInit() {
  }
  ngAfterViewInit(){
    
  }
  public CONTROL_TYPE = controlType
  @Input() inputData: formInput;
  @Input() form: FormGroup;
  get isValid() { 
    let control = this.form.controls[this.inputData.key]
    // if(control){
      return this.form.controls[this.inputData.key].valid; 
    // }
  }
  get getDirtyTouch(){
    // if(this.form.controls[this.inputData.key]){
      return this.form.controls[this.inputData.key].touched
    // }
  }
  get isNumber(){
    let errors = this.form.controls[this.inputData.key].errors
    if(errors&&errors.number){
      return false
    }else{
      return true
    }
  }
  get errorsType(){
    let control = this.form.controls[this.inputData.key]
    let errors = this.form.controls[this.inputData.key].errors
    if(errors&&errors.number){
      return this.CONTROL_TYPE.NUMBER
    }
    if(this.inputData.controlType == this.CONTROL_TYPE.EnglishAndNumber){
      if(errors&&errors.pattern){
        return this.CONTROL_TYPE.EnglishAndNumber
      }
    }
    return "required"
  }
}
